package org.bilinc.gz001;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Lalasianin
 * Date: 19.11.12
 * Time: 19:04
 * To change this template use File | Settings | File Templates.
 */
public class UiTableData extends AbstractTableModel {

    private List<List<String>> mTableData = new LinkedList<List<String>>();
    private List<String> mColumns = new ArrayList<String>();

    public UiTableData() {

    }

    public List<List<String>> getTableData() {
        return mTableData;
    }

    public void setTableData(List<List<String>> tableData) {
        mTableData = tableData;
    }

    public void setColumns(String[] columns) {
        for (int i = 0; i < columns.length; i++) {
            mColumns.add(columns[i]);
        }
    }

    public void clear() {
        mTableData.clear();
    }

    public void addRow(List<String> row) {
        mTableData.add(row);
    }

    public void removeRow(int rowIndex) {
        mTableData.remove(rowIndex);
    }

    @Override
    public int getRowCount() {
        return mTableData.size();
    }

    @Override
    public String getColumnName(int column) {
        return mColumns.get(column);
    }

    @Override
    public int getColumnCount() {
        return mColumns.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return mTableData.get(rowIndex).get(columnIndex);
    }

    @Override
    public void setValueAt(Object obj, int row, int column) {
        mTableData.get(row).set(column, (String) obj);
    }

    public String getCell(int rowIndex, int columnIndex) {
        return mTableData.get(rowIndex).get(columnIndex);
    }

    public String setCell(String value, int rowIndex, int columnIndex) {
        return mTableData.get(rowIndex).set(columnIndex,  value);
    }

    public void setRow(List<String> row, int rowIndex) {
        mTableData.set(rowIndex, row);
    }

    public void addCellToRow(String value, int rowIndex) {
        mTableData.get(rowIndex).add(value);
    }

    public List<String> getRow(int row) {
        return mTableData.get(row);
    }

    public void insertRow(int row, List<String> data) {
        List<List<String>> temp = new ArrayList<List<String>>();
        temp.addAll(mTableData.subList(0, row+1));
        temp.add(data);
        temp.addAll(mTableData.subList(row+1, mTableData.size()));
        mTableData.clear();
        mTableData = temp;
    }
}
